package com.sina.shop.manage;

import com.sina.cloudstorage.services.scs.SCS;
import com.sina.cloudstorage.services.scs.SCSClient;
import com.sina.shop.util.Constant;
import com.sina.shop.web.resolver.JsonViewExceptionResolver;
import com.sina.shop.manage.config.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.servlet.View;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-11-10-17:03
 */
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@ServletComponentScan({"com.sina.shop.web.filter"})
@Import({ServiceConfig.class, JsonViewContextConfig.class, WebCustomConfig.class, ShiroConfigurations.class})
@PropertySource(name = "sinaStorage", value = {"classpath:/config/sinaStorage.properties"})
@EnableConfigurationProperties
public class ManageApplication extends SpringBootServletInitializer implements ServletContextAware, ApplicationContextAware {


    private ServletContext servletContext;

    private ApplicationContext applicationContext;

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.servletContext = servletContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static void main(String[] args) {
        SpringApplication.run(ManageApplication.class, args);
    }

    @Bean
    public SCS sinaStorage() {
        return new SCSClient();
    }


    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize(1024L * 1024L * 1024L);
        factory.setMaxRequestSize(1024L * 1024L * 1024L);
        return factory.createMultipartConfig();
    }

    /**
     * capture  controller exception
     */
    @Bean
    @Autowired
    public JsonViewExceptionResolver jsonViewExceptionResolver(
            @Qualifier("webLayerMessageSource") ResourceBundleMessageSource webLayerMessageSource,
            @Qualifier("resultView") View resultView) {
        JsonViewExceptionResolver jsonViewExceptionResolver = new JsonViewExceptionResolver();
        jsonViewExceptionResolver.setMessageSource(webLayerMessageSource);
        jsonViewExceptionResolver.setDefaultView(resultView);
        return jsonViewExceptionResolver;
    }


    @Bean
    public ResourceBundleMessageSource webLayerMessageSource(
            @Qualifier("serviceLayerMessageSource")
                    ResourceBundleMessageSource serviceLayerMessageSource) {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setUseCodeAsDefaultMessage(true);
        resourceBundleMessageSource.setDefaultEncoding(Constant.UTF8);
        resourceBundleMessageSource.setBasename("i18n/web/messages");
        resourceBundleMessageSource.setParentMessageSource(serviceLayerMessageSource);
        return resourceBundleMessageSource;
    }


    @Bean
    public MessageSourceAccessor defaultWebLayerMessageSourceAccessor(
            @Qualifier("webLayerMessageSource") ResourceBundleMessageSource webLayerMessageSource) {
        // 若有必要将来可 更改 messageSource
        return new MessageSourceAccessor(webLayerMessageSource, Locale.SIMPLIFIED_CHINESE);
    }

}